<?php

if (!defined('DIR_SEP'))
    define('DIR_SEP', DIRECTORY_SEPARATOR);

require_once('./modules/pages/loader.inc');

class ManagePagesRenderer extends Renderer
{
	protected $success;
	
	function pageLoad()
	{
		switch( $this->action )
		{
			case "add_page":
				$this->addPage();
				break;
			case "del_page":
				$this->delPage();
				break;
			case "move_page":
				$this->movePage();
				break;
			case "":
			case "show":
			default:
				break;
		}
	}

	function delPage()
	{
		$page = $_GET['pageId'];
		
		PagesLoader::delPage($page);

		die('page $page deleted successfully');
	}

	function movePage()
	{
		$page1 = $_GET['pageId1'];
		$page2 = $_GET['pageId2'];
		
		$fileInfo = PagesLoader::loadPage($page1);
		$fileInfo['position'] = $fileInfo['position'] -1 < 0 ? 0 : $fileInfo['position'] -1;
		PagesLoader::savePageInfo($page1,$fileInfo);
		
		$fileInfo = PagesLoader::loadPage($page2);
		$fileInfo['position'] = $fileInfo['position'] + 1;
		PagesLoader::savePageInfo($page2,$fileInfo);

		die('pages $page1,$page2 moved successfully');
	}
	
	function addPage()
	{
		$page = $_POST['txtNewPage'];

		//we don't override an existing one...
		$this->success = PagesLoader::addPage($page);
	}
	
	function isPermitted()
	{
		//echo "permission check feeds";
		if ( isset($this->user) && $this->user->type >= 1 )
			return true;
		else
			return false;
	}
	
	function renderContent()
	{
		global $content;
				
		$content .= 
		"<script src='./script/popup.js'></script>
		<script>
			function askDelete(pageId)
			{
				_popup.show('Are you sure you wish to delete '+pageId+'?',delPage,pageId);
			}
			
			function move(pageId,isUp)
			{
				var curr = $('#tr'+pageId);
				var next = curr.next();

				if( isUp )
				{
					next = curr;
					curr = curr.prev();
				}

				var reqUrl = document.location.href.replace(/\?.*/ig,\"?pg=manage_pages&act=move_page&pageId1=\"+next.attr('id').substring(2)+\"&pageId2=\"+curr.attr('id').substring(2));
				$.get(reqUrl);
				
				if(typeof(curr) != 'undefined' && typeof(next) != 'undefined')
					next.insertBefore(curr);
				
			}
			
			function delPage(isYes,pageId)
			{
				if(isYes)
				{
					var reqUrl = document.location.href.replace(/\?.*/ig,\"?pg=manage_pages&act=del_page&pageId=\"+pageId);
					$.get(reqUrl);
					$('#tr'+pageId).remove();
				}
				
				_popup.hide();
			}
		</script>
		<p><h1>Manage pages</h1></p>
		<div style='margin-bottom:20px;'>
			<form action='index.php?pg=manage_pages&act=add_page' method='POST'>
				<style>.fieldTitle { width:60px;}</style>
				<fieldset style='width:380px;'>
					<legend>New Page:</legend>
					<label class=\"fieldTitle mandatory\">name:</label><input class='text' id='txtNewPage' name='txtNewPage' type='text'/>
					<input class='submit' type='submit' value='Create'/>
				</fieldset>
			</form>";
		
		if($this->action != '' && $this->success)
			$content .= "<P/><label style='color:green;'>Page added successfully</label>";
		else if ($this->action != '') 
			$content .= "<P/><label style='color:red;'>Failed to add page</label>";
		
		$content .="</div>
		<div>
			<table id='tFeeds' style='table-layout:fixed;border-collapse:collapse;border-color:#BBB;' border='1'>
				<thead>
					<tr>
						<th style='width:220px'>Name</th>
						<th style='width:20px'><img src='./images/blank.gif'/></th>
						<th style='width:20px'><img src='./images/blank.gif'/></th>
						<th style='width:20px'><img src='./images/blank.gif'/></th>
						<th style='width:20px'><img src='./images/blank.gif'/></th>
					</tr>
				</thead>
				<tbody>";

		$files = PagesLoader::listPages();
		
		if (!isset($files) || count($files) == 0) 
			$content .="<tr><td colspan='1'>No pages</td></tr>";
		else
		{
			foreach($files as $fileInfo)
			{
				$redirect = "document.location.href='index.php?pg=edit_page&pageId=".$fileInfo['filename']."'";
				
				$content .= "<tr id='tr".$fileInfo['filename']."'>
								<td style='width:220px'>".$fileInfo['filename']."</td>
								<td style='width:20px;cursor:pointer;'><img src='./images/pencil.png' onclick=\"$redirect\"></td>
								<td style='width:20px;cursor:pointer;'><img src='./images/delete.png' onclick=\"askDelete('".$fileInfo['filename']."');\"></td>
								<td style='width:20px;cursor:pointer;'><img src='./images/up.png' onclick=\"move('".$fileInfo['filename']."',1);\"></td>
								<td style='width:20px;cursor:pointer;'><img src='./images/down.png' onclick=\"move('".$fileInfo['filename']."',0);\"></td>
								</tr>";
			}
		}
		$content .=	"</tbody>
			</table>
		</div>";
	}
}

$renderer = new ManagePagesRenderer();

?>